﻿using System.Collections.Generic;

namespace Raven
{
    public class EntityPool
    {
        private Dictionary<EntityType, Queue<BaseGameEntity>> _pool =
            new Dictionary<EntityType, Queue<BaseGameEntity>>();

        public void Clear()
        {
            foreach (var kv in _pool)
            {
                foreach (var entity in kv.Value)
                    entity.Destroy();
            }

            _pool.Clear();
        }

        public int count
        {
            get { return _pool.Count; }
        }

        public T GetEntity<T>(EntityType type) where T : BaseGameEntity
        {
            Queue<BaseGameEntity> arr;
            if (_pool.TryGetValue(type, out arr) && arr.Count > 0)
            {
                return arr.Dequeue() as T;
            }

            return null;
        }

        public void ReturnEntitiy(BaseGameEntity entity)
        {
            Queue<BaseGameEntity> arr;
            if (!_pool.TryGetValue(entity.GetEntityType(), out arr))
            {
                arr = new Queue<BaseGameEntity>();
                _pool.Add(entity.GetEntityType(), arr);
            }

            arr.Enqueue(entity);
        }
    }
}